﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class search_invoice : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }        
        this.EnableViewState = false;
        Response.Write(Init.InitIndex(EnterpriseName, UserPower));
        Footer = Init.IndexFooter();
        djbh = Request.QueryString["d"];
        pihao = Request.QueryString["n"];
        keyword = Request.QueryString["k"];
        if (!string.IsNullOrEmpty(djbh) || !string.IsNullOrEmpty(keyword))
        {
            if (UserPower.Contains("invoice"))
            {
                Literal1.Text = DataList(djbh, keyword);
            }
            else
            {
                Literal1.Text = "您没有权限查询两票制票据，请联系质量管理部或业务部";
            }
        }
    }
	
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string djbh = "";
    public string pihao = "";
    public string keyword = "";
	
    public DataSet DataGroup(string djbh = "", string keyword = "")
    {
        string sql = "select cast(case when (select count(imageid) from [ImageDoc](nolock) where beactive='是' and detailid=left(rtrim(a.pici),14) and type_id in('fa','sh','ghssh','ghsfa','rk'))>0 then a.plh else '' end as varchar(20)) as plh,"
            + " rtrim(a.plh) as plhup,a.remind_fp,"
         + " rtrim(a.spid) as spid,rtrim(a.djbh) as djbh,rtrim(a.pihao) as pihao,left(rtrim(a.pici),14) as rkdh,"
         + "rtrim(b.spbh) as spbh,rtrim(b.spmch) as spmch,rtrim(b.shpgg) as shpgg,rtrim(b.dw) as dw,rtrim(b.shengccj) as shengccj,rtrim(b.pizhwh) as pizhwh,'' as url "
         + " from [spls_ck] a(nolock),[spkfk] b(nolock) where a.spid=b.spid ";
        sql += (UserPower.Contains("Manager"))?"":" and a.dwbh in(select dwbh from [mchk](nolock) where dwmch='"+EnterpriseName+"')";
        sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='" + djbh + "' " : "";
        sql += (!string.IsNullOrEmpty(keyword)) ? " and (b.spbh like '%" + keyword + "%' or b.spmch like '%" + keyword + "%' or b.zjm like '%" + keyword + "%')" : "";
        sql += " order by dwbh desc";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);
        for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
        {
            dss.Tables[0].Rows[i]["url"] = herf(dss.Tables[0].Rows[i]["rkdh"].ToString(), dss.Tables[0].Rows[i]["plhup"].ToString(), dss.Tables[0].Rows[i]["remind_fp"].ToString());            
        }
        return dss;
    }
    public string DataList(string djbh = "", string keyword = "")
    {
        string result = "";
        result += "<table border=\"1\" style=\"border:1px solid #ccc;width:100%;\">\r\n"
                + "<tbody>\r\n"
                + "<tr height=\"50\" style=\"background: #eee;text-align: center\">\r\n"
                + "<td width=\"60\" style=\"text-align: left;padding-left:10px;\"><label class=\"searxh-content-label\"><input class=\"check_all\" type=\"checkbox\">全选</label></td>\r\n"
                + "<td>编号</td><td>名称</td><td width=\"77\">规格</td><td width=\"78\">单位</td><td>生产厂家</td><td width=\"96\">批准文号</td><td>批号</td><td>操作</td>\r\n"
                + "</tr>";

        DataSet ds = DataGroup(djbh, keyword);
        DataTable dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            result += "<tr height=\"50\" style=\"text-align: center\">\r\n"
                    + "<td><label class=\"searxh-content-label\"><input type=\"checkbox\" ";
            result += dt.Rows[i]["plh"].ToString().Trim().ToLower() == "" ? "disabled=\"disabled\"" : "name=\"ids\" value=\"" + dt.Rows[i]["plh"];
            result += "\" />" + (i + 1).ToString() + "</label></td>\r\n"
                    + "<td>" + dt.Rows[i]["spbh"] + "</td>\r\n"
                    + "<td class=\"drug_name\">" + dt.Rows[i]["spmch"] + "</td>\r\n"
                    + "<td class=\"drug_point\">" + dt.Rows[i]["dw"] + "</td>\r\n"
                    + "<td class=\"drug_rule\">" + dt.Rows[i]["shpgg"] + "</td>\r\n"
                    + "<td class=\"drug_changjia\">" + dt.Rows[i]["shengccj"] + "</td>\r\n"
                    + "<td class=\"drug_batch_number\">" + dt.Rows[i]["pizhwh"] + "</td>\r\n"
                    + "<td class=\"drug_batch_number\">" + dt.Rows[i]["pihao"] + "</td>\r\n"
                    + "<td>" + dt.Rows[i]["url"] + "</td>\r\n"
                    + "</tr>\r\n";
        }
        result += "<tr height=\"50\">\r\n"
                 + "<td colspan=\"9\" style=\"text-align:left;padding-left:10px;\">\r\n"
                 + "<label class=\"searxh-content-label\" style=\"margin-right:10px;\"><input class=\"check_all\" type=\"checkbox\">全选</label>\r\n"
                 + "<a class=\"btn-down-all operation\" data-opt=\"download\" data-optio=\"fa\" href=\"#\">发票下载所选</a>\r\n"
                 + "<a class=\"btn-print-all operation\" data-opt=\"print\" data-optio=\"fa\" href=\"#\">发票打印所选</a>\r\n"
                 + "<a class=\"btn-down-all operation\" data-opt=\"download\" data-optio=\"sh\" href=\"#\">随货同行单下载所选</a>\r\n"
                 + "<a class=\"btn-print-all operation\" data-opt=\"print\" data-optio=\"sh\" href=\"#\">随货同行单打印所选</a>\r\n"
                 + "<a class=\"btn-down-all operation\" data-opt=\"download\" data-optio=\"rk\" href=\"#\">入库单下载所选</a>\r\n"
                 + "<a class=\"btn-print-all operation\" data-opt=\"print\" data-optio=\"rk\" href=\"#\">入库单打印所选</a>\r\n"
                 + "<a class=\"btn-down-all operation\" data-opt=\"download\" data-optio=\"iquanbu\" href=\"#\">全部下载</a>\r\n"
                 + "<a class=\"btn-print-all operation\" data-opt=\"print\" data-optio=\"iquanbu\" href=\"#\">全部打印</a>\r\n"
                 + "</td>\r\n"
                 + "</tr>\r\n"
                 + "</table>\r\n";
        return result;
    }
    public string herf(string id, string ret, string remind)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "";

        sql = "select rtrim(imageid) as imageid,rtrim(url) as url,rtrim(type_id) as type_id from [ImageDoc](nolock) where beactive='是' and detailid=rtrim('" + detailid + "') and type_id in('fa','sh','ghssh','ghsfa','rk')";
        dsmx = mssql.ExecuteDs(sql);
        if (dsmx.Tables[0].Rows.Count > 0)
        {
            retsult = retsult + "<a class=\"btn-down operation\" href=\"#\" data-opt=\"download\" data-optio=\"iquanbu\"  data-id=\"" + ret + "\">下载</a>";
            retsult = retsult + "<a class=\"btn-print operation\" href=\"#\" data-opt=\"print\" data-optio=\"iquanbu\"  data-id=\"" + ret + "\">打印</a>";
            int rowid=0;
			for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
                string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + id.ToString().Trim().ToLower() + "\" " + (rowid == 0 ? "" : " style=\"display:none;\"") + ">预览</a>\r\n";
					rowid++;
                }
                /*
                 if (i == 0)
                {
                    retsult = retsult + " <a class=\"btn-view preview\" href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" rel=\"" + id.ToString().Trim().ToLower() + "\">预览</a>";
                }
                else
                {
                    retsult = retsult + " <a class=\"btn-view preview\" href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" rel=\"" + id.ToString().Trim().ToLower() + "\" style=\"display:none;\">预览</a>";
                }
                 */
            }
        }
        else
        {
            retsult += " <a class=\"btn-tixing tixing\" href=\"#\" data-id=\"" + ret + "\" data-optio=\"fp\">";
            retsult += remind.ToString() == "1" ? "<font color=\"#ff00\">已提醒</font>" : "提醒上传";
            retsult += "</a>\r\n";
        }
        return retsult;
    }
}